package com.itheima.service.impl;


import com.itheima.dto.*;
import com.itheima.entity.AllData;
import com.itheima.entity.Group;
import com.itheima.entity.update;
import com.itheima.mapper.ConfigMapper;
import com.itheima.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;


@Slf4j
@Service
public class ConfigServiceImpl implements ConfigService {

    private final ConfigMapper configMapper;

    public ConfigServiceImpl(ConfigMapper configMapper) {
        this.configMapper = configMapper;
    }

    @Override
    public ConfigDTO getAll() {
        Set<String> set = configMapper.getTob();
        ConfigDTO configDTO = new ConfigDTO();
        List<ConfigurationDTO_Data> list = new ArrayList<>();
        for (String s : set) {
            list.add(getList(s));
            configDTO.setList(list);
        }

        List<AllData> allData = configMapper.getAttr();
        List<AllDataDTO> allDataDTOList = new ArrayList<>();
        allData.forEach(s -> allDataDTOList.add(s.getAllDataDTO()));
        configDTO.setAllData(allDataDTOList);

        List<String> configMap = configMapper.getMap();
        Map<String, String> map = new HashMap<>();
        for (String s : configMap) {
            String getValue = configMapper.getValue(s);
            map.put(s, getValue);
        }
        configDTO.setConfigForm(map);

        Map<String, String> RulesMap = new HashMap<>();
        RulesMap.put("site_name", "[{\"required\":true,\"message\":\"站点名必须\",\"trigger\":\"blur\"}]");
        RulesMap.put("dolore exercitation officia eu", "3");
        configDTO.setConfigRules(RulesMap);
        return configDTO;
    }

    @Override
    public void update(List<updateDTO> updateDTO) {
        List<update> updates = new ArrayList<>();
        updateDTO.forEach(d -> updates.add(d.getUpdateVO()));
        System.out.println(updates);
        updates.forEach(configMapper::update);
    }

    private ConfigurationDTO_Data getList(String s) {
        ConfigurationDTO_Data cd = new ConfigurationDTO_Data();
        Set<String> setTag = configMapper.getGroupValue(s);
        switch (s) {
            case "base":
                cd.setName("基础配置");
                break;
            case "custom":
                cd.setName("其他配置");
                break;
            case "system":
                cd.setName("系统配置");
                break;
        }
        cd.setKey(s);
        List<SubseDTO> list1 = new ArrayList<>();
        for (String str : setTag) {
            SubseDTO subseDTO = new SubseDTO();
            subseDTO.setName(str);
            List<Group> strGroup = configMapper.getAllGroup(str, s);
            List<GroupDTO> groupDTOS = new ArrayList<>();
            strGroup.forEach(e -> {
                groupDTOS.add(e.getGroupDTO());
            });
            subseDTO.setData(groupDTOS);
            if (!subseDTO.getData().isEmpty()) {
                list1.add(subseDTO);
            }
        }
        cd.setChildren(list1);
        return cd;
    }
}
